{extend name="template/edit" /}
{block name="content"}
<div class="page-container">
        <div class="pt-5 pr-5 pos-f" style="right: 20px;top: 20px;">
            <button type="button" class="btn btn-primary radius save">&nbsp;&nbsp;保存&nbsp;&nbsp;</button>
            <button type="button" class="ml-20 btn btn-default radius" onclick="layer_close()">&nbsp;&nbsp;取消&nbsp;&nbsp;</button>
        </div>
    <div id="tree"></div>
</div>
{/block}
{block name="script"}
<script type="text/javascript" src="__LIB__/Validform/5.3.2/Validform.min.js"></script>
<script type="text/javascript" src="/static/area/area.min.js?v=1.0"></script>
<link rel="stylesheet" href="__LIB__/wdTree/css/tree.css">
<script type="text/javascript" src="__LIB__/wdTree/jquery.tree.js"></script>
<script>
    $(function () {
        $('.skin-minimal input').iCheck({
            checkboxClass: 'icheckbox-blue',
            radioClass: 'iradio-blue',
            increaseArea: '20%'
        });
		var tree = [];
		var checkName = {$check_name|default=''};//选中地区name集
		WYArea.init(function(){
			province = WYArea.getProv();
			$(province).each(function(){
				isTe = false;//是否直辖市
				provinceSelf = this;
				provinceJson = WYArea.parser(provinceSelf);//{id : 170800,grade : 1,name : 梅州,py : meizhou,letter : M}
				prid = provinceJson.code;
				prname = provinceJson.name;
				checkProvState = 0;//省份默认没选中
                        city = WYArea.getCity(prid);
                        if (!city) {
                            city = [provinceSelf];
                            isTe = true;
                        }
                        cityNodes = [];
                        $(city).each(function () {
                            checkCityState = 0;//省份默认没选中
                            citySelf = this;
                            cityJson = WYArea.parser(citySelf);
                            cid = cityJson.code;
                            if (cid == prid) {
                                cid = cid + 100;//如果是直辖市，则城市ID自增100，一遍js识别元素ID
                            }
                            cname = cityJson.name;
                            area = WYArea.getArea(cid);
                            areaNodes = [];
                            $(area).each(function () {
                                areaSelf = this;
                                areaJson = WYArea.parser(areaSelf);
                                aid = areaJson.code;
                                aname = areaJson.name;
                                currentName = '';//当前地区节点全名
                                if (isTe) {
                                    currentName = prname + aname;//直辖市，当前城市节点为直辖市名+区：北京朝阳区
                                } else {
                                    currentName = prname + cname + aname;//当前城市节点为省+市+区：广东广州花都区
                                }
                                checkAreaState = checkName.indexOf(currentName) == -1 ? 0 : 1;
                                if (checkAreaState) {
                                    checkCityState = 1;//若区有选中的，则该城市肯定选中
                                    checkProvState = 1;//若区有选中的，则该省份肯定选中
                                }
                                if (cid == prid) {
                                    cid = cid + 100;//如果是直辖市，则城市ID自增100，以便js识别元素ID
                                }
                                areaNodes.push({
                                    id: aid,
                                    text: aname,
                                    value: aid,
                                    showcheck: true,
                                    checkstate: checkAreaState,
                                    hasChildren: false,
                                    isexpand: false,
                                    complete: true
                                });
                            });
                            cityNodes.push({
                                id: cid,
                                text: cname,
                                value: cid,
                                showcheck: true,
                                checkstate: checkCityState,
                                hasChildren: true,
                                isexpand: false,
                                complete: true,
                                ChildNodes: areaNodes
                            });
                        });
                        tree.push({
                            id: prid,
                            text: prname,
                            value: prid,
                            showcheck: true,
                            checkstate: checkProvState,
                            hasChildren: true,
                            isexpand: false,
                            complete: true,
                            ChildNodes: cityNodes
                        });
                    });

                    $("#tree").treeview({
                        showcheck: true,
                        cbiconpath: '__LIB__/wdTree/css/images/icons/',
                        data: tree,
                        theme: 'bbit-tree-lines',
                    });
                });
                $(function () {

                    $(".save").click(function () {
                        var node_id = $("#tree").getCheckedNodes();
                        node = [];
                        ids = [];
                        $(node_id).each(function () {
                            nodeJson = WYArea.getPath(this.slice(0, 6));
                            prov = this.slice(0, 2);
                            specialProv = ['11', '12', '13', '14', '43', '44', '45'];//直辖市省份ID
                            id = '';
                            name = '';
                            if (nodeJson.length > 2 || (nodeJson.length == 2 && specialProv.indexOf(prov) > -1)) {
                                //选中的直辖市或者城市才做处理
                                $(nodeJson).each(function () {
                                    _self = this;
                                    node_sub = WYArea.parser(_self);
                                    if (id == '') {
                                        id = node_sub.code;
                                    }
                                    name = node_sub.name + name;
                                });
                                if (ids.indexOf(id) < 0) {
                                    //每个城市，区只需存一条数据
                                    node.push({spec_id: id, spec_name: name});
                                    ids.push(id);
                                }
                            }
                        });
                        ajax_req('{:\\think\\Request::instance()->baseUrl()}', {sp_id: '{$Request.param.sp_id}', class_id: '{$Request.param.class_id}', store_id: '{$Request.param.store_id}', 'node': node}, undefined, undefined, true);
                    });
                    alert(node);
                })
            })
</script>
{/block}